<template>
  <div class="demo">
    <h2>基本用法</h2>
    <view class="demo-box">
      <nut-swiper :init-page="page" :pagination-visible="true" pagination-color="#426543" auto-play="2000">
        <nut-swiper-item v-for="item in list" :key="item">
          <img :src="item" alt="" />
        </nut-swiper-item>
      </nut-swiper>
    </view>
    <h2>异步加载(3s)</h2>
    <view class="demo-box">
      <nut-swiper :init-page="page" :pagination-visible="true" pagination-color="#426543" auto-play="2000">
        <nut-swiper-item v-for="item in list1" :key="item">
          <img :src="item" alt="" />
        </nut-swiper-item>
      </nut-swiper>
    </view>
    <h2>动态加载</h2>
    <view class="demo-box">
      <nut-swiper :init-page="page" :pagination-visible="true" pagination-color="#426543" auto-play="2000">
        <nut-swiper-item v-for="item in list2" :key="item">
          <img :src="item" alt="" />
        </nut-swiper-item>
      </nut-swiper>
    </view>
    <h2>自定义大小</h2>
    <view class="demo-box">
      <nut-swiper :init-page="page2" :loop="false" width="300">
        <nut-swiper-item v-for="item in list" :key="item">
          <img :src="item" alt="" />
        </nut-swiper-item>
      </nut-swiper>
    </view>
    <h2>自定义指示器</h2>
    <view class="demo-box">
      <nut-swiper :init-page="page3" :loop="true" @change="change">
        <nut-swiper-item v-for="item in list" :key="item">
          <img :src="item" alt="" />
        </nut-swiper-item>
        <template v-slot:page>
          <div class="page"> {{ current }}/4 </div>
        </template>
      </nut-swiper>
    </view>
    <h2>自定义指示器(异步加载3s)</h2>
    <view class="demo-box">
      <nut-swiper :init-page="page" :loop="true" @change="change1" auto-play="2000">
        <nut-swiper-item v-for="item in list1" :key="item">
          <img :src="item" alt="" />
        </nut-swiper-item>
        <template v-slot:page>
          <div class="page"> {{ current1 }}/4 </div>
        </template>
      </nut-swiper>
    </view>
    <h2>手动切换</h2>
    <view class="demo-box">
      <nut-swiper :init-page="page" :loop="true" ref="swiper">
        <nut-swiper-item v-for="item in list" :key="item">
          <img :src="item" alt="" />
        </nut-swiper-item>
      </nut-swiper>
      <view class="nut-swiper-btns">
        <span class="nut-swiper-btns__left" @click="handlePrev">
          <nut-icon name="left"></nut-icon>
        </span>
        <span class="nut-swiper-btns__left" @click="handleNext">
          <nut-icon name="right"></nut-icon>
        </span>
      </view>
    </view>
    <h2>垂直方向</h2>
    <view class="demo-box">
      <nut-swiper
        :init-page="page4"
        :loop="true"
        auto-play="3000"
        direction="vertical"
        height="150"
        :pagination-visible="true"
        style="height: 150px"
      >
        <nut-swiper-item v-for="item in list" :key="item">
          <img :src="item" alt="" />
        </nut-swiper-item>
      </nut-swiper>
    </view>
  </div>
</template>

<script lang="ts">
import { reactive, toRefs, onMounted, ref, Ref } from 'vue';

export default {
  props: {},
  setup() {
    const swiper = ref(null) as Ref;
    const state = reactive({
      page: 2,
      page2: 0,
      page3: 0,
      page4: 0,
      current: 1,
      current1: 3,
      list: [
        'https://storage.360buyimg.com/jdc-article/NutUItaro34.jpg',
        'https://storage.360buyimg.com/jdc-article/NutUItaro2.jpg',
        'https://storage.360buyimg.com/jdc-article/welcomenutui.jpg',
        'https://storage.360buyimg.com/jdc-article/fristfabu.jpg'
      ],
      list2: [
        'https://storage.360buyimg.com/jdc-article/NutUItaro34.jpg',
        'https://storage.360buyimg.com/jdc-article/NutUItaro2.jpg',
        'https://storage.360buyimg.com/jdc-article/welcomenutui.jpg',
        'https://storage.360buyimg.com/jdc-article/fristfabu.jpg'
      ],
      list1: [] as string[]
    });
    const change = (index: number) => {
      state.current = index + 1;
    };
    const change1 = (index: number) => {
      state.current1 = index + 1;
    };
    const handlePrev = () => {
      swiper.value.prev();
    };
    const handleNext = () => {
      swiper.value.next();
    };
    onMounted(() => {
      setTimeout(() => {
        state.list1 = state.list.slice();
        state.list2.splice(1, 1);
      }, 3000);
    });
    return {
      ...toRefs(state),
      swiper,
      change,
      change1,
      handlePrev,
      handleNext
    };
  }
};
</script>

<style lang="scss">
.demo-box {
  position: relative;
  .nut-swiper-item {
    height: 150px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .nut-swiper-pagination-vertical {
    i {
      width: 6px;
      height: 6px;
      border-radius: 50%;
      &.active {
        height: 18px;
        border-radius: 5px;
      }
    }
  }
  .page {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 46px;
    height: 22px;
    background: rgba(0, 0, 0, 0.33);
    border-radius: 22px;
    text-align: center;
    color: #fff;
    font-size: 14px;
  }
  .nut-swiper-btns {
    width: 100%;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    z-index: 1;
    display: flex;
    justify-content: space-between;
    span {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 20px;
      height: 30px;
      background-color: rgba(0, 0, 0, 0.2);
    }
  }
}
</style>
